﻿//===============================================================================
//	StringFactory.cpp
//	字符串工厂
//	静态工厂。所有std::string相关字符串从此处生成。申请使用完毕后，需要释放。
//===============================================================================

#include "StringFactory.h"

#include "user_define.h"
#include "log.h"

//-------------------------------------------------------------------------------
using namespace string_factory;

//-------------------------------------------------------------------------------
using StringFactoryPtr = std::shared_ptr<StringFactory>;
StringFactoryPtr g_string_factory_ptr = std::make_shared<StringFactory>();

//-------------------------------------------------------------------------------
std::string * string_factory::CreateString(const char * str)
{
	_safe_guard_begin;

	return g_string_factory_ptr->CreateString(str);

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
bool string_factory::FreeString(std::string * sp)
{
	_safe_guard_begin;

	return g_string_factory_ptr->FreeString(sp);

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
StringFactory::StringFactory(void)
{
}

//-------------------------------------------------------------------------------
StringFactory::~StringFactory(void)
{
}

//-------------------------------------------------------------------------------
std::string * StringFactory::CreateString(const char * str)
{
	_safe_guard_begin;
	
	std::lock_guard<std::recursive_mutex> _lock(m_mutex);
	
	std::string* _sp = m_string_pool.construct(str);
	return _sp;

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
bool StringFactory::FreeString(std::string * sp)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> _lock(m_mutex);

	//printf("StringFactory::FreeString %s\n", sp->c_str());

	if (m_string_pool.is_from(sp))
	{
		m_string_pool.destroy(sp);
		sp = nullptr;

		return true;
	}

	return false;

	_safe_guard_end;

	return false;
}

//-------------------------------------------------------------------------------
void string_factory::StringFactory::PurgeMemory(void)
{
	_safe_guard_begin;

	_safe_guard_end;
}


//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------